﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LOGISTCBUS.DAO
{
    public class BaseDAO
    {
        #region protected methods
        protected String getConditions(String value, params String[] columns)
        {
            if (String.IsNullOrEmpty(value) || columns == null || columns.Length == 0)
                return "1=1";

            String conditions = "";
            String[] values = value.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (String v in values)
            {
                if (String.IsNullOrEmpty(v))
                    continue;

                conditions += "(";
                foreach (String col in columns)
                    conditions += String.Format("{0} like '%{1}%' or ", col, v.Replace("'", "''").Replace("?", "\\?"));
                conditions = conditions.Remove(conditions.Length - 4) + ") and ";
            }
            return conditions.Length <= 5 ? "1=1" : conditions.Remove(conditions.Length - 5);
        }

        protected String getSQLDeleteUpdate(String table, String column)
        {
            return String.Format("update {0} set removed=((select max(cast((t2.removed) as int)) from {0} t2 where t2.{1}= {0}.{1}) + 1) where id= @id", table, column);
        }
        #endregion
    }
}
